<?php
/**
 * Created by PhpStorm.
 * User: Min
 * Date: 2018/4/20
 * Time: 14:23
 */

namespace SDXApi\Service;


use Think\Cache\Driver\Redis;

class UserBonusService
{
    /**
     * @var Redis
     */
    protected $queue;

    /**
     * UserBonusService constructor.
     */
    public function __construct()
    {
        $this->queue = new Redis();
    }

    /**
     * @author Min
     * @param $user_id
     * @param $bonus_type_id
     * @param $bonus_data
     */
    public function handleQueue($user_id, $bonus_type_id, $bonus_data)
    {

        if($this->queue->is_exists("bonus_user_id_seat_".$bonus_type_id."_child_".$user_id)){
            returnApiError('您已领过该红包，请到“我的 => 红包”查看');
        }

        $is_get=$this->queue->setnx("auto_get_bonus_".$user_id.'_'.$bonus_type_id, $bonus_type_id, 3);
        if(!$is_get){
            returnApiError('请勿重复操作');
        }

        $bonusCanGetNum = $bonus_data['bonusCanGetNum'];

        if($this->queue->is_exists("bonus_status_".$bonus_type_id)){
            returnApiError('来晚一步，红包已被领完!');
        }

        for($i=1;$i<=$bonusCanGetNum;$i++){
            if(!$this->queue->is_exists("bonus_seat_".$bonus_type_id."_child_".$i)){
                $is_sit_down=$this->queue->setnx("bonus_seat_".$bonus_type_id."_child_".$i, $i);
                if($is_sit_down){
                    //最后一个座位被占后
                    if($i==$bonusCanGetNum){
                        $this->queue->set("bonus_status_".$bonus_type_id,"Red War Is Over");
                    }
                    break;
<<<<<<< .mine
                }else{
                    returnApiError('红包被别人抢走了，再试试手气！');
                }
||||||| .r5741
                }
=======
                }else{
					returnApiError('很遗憾，被人抢先了一步！');
				}
>>>>>>> .r5767
            }

            if($i==$bonusCanGetNum){
                returnApiError('来晚一步，红包已被领完!!');
            }

        }
        
        $queue_key = $this->getQueueKey($bonus_type_id);
        $bonus_id = $this->queue->lPop($queue_key);

        if (! $bonus_id) {
            returnApiError('来晚一步，红包已被领完!!!');
        }else{
            $queue_key_wait = $this->getQueueKey_wait();
            $this->queue->rPush($queue_key_wait, json_encode(array($bonus_type_id,$user_id,$bonus_id)));
            $data['action'] = 1;
            $data['bonus_id'] = $bonus_id;
<<<<<<< .mine
            returnApiSuccess($data,"恭喜您，抢到红包啦！");
||||||| .r5741
            returnApiSuccess($data,"领取成功");
=======

            $this->queue->setnx("bonus_user_id_seat_".$bonus_type_id."_child_".$user_id, $user_id);

            returnApiSuccess($data,"恭喜您，抢到红包啦！");
>>>>>>> .r5767
        }


    }

    /**
     * @author Min
     * @param $bonus_type_id
     * @return bool
     *
     */
    public function addBonusToqueue($bonus_type_id)
    {
        if (empty($bonus_type_id)) {
            return false;
        }

        $bonus = $this->getBonusList($bonus_type_id);

        $queue_key = $this->getQueueKey($bonus_type_id);

        //删除就数据
        $this->queue->rm($queue_key);

        $this->queue->rm("bonus_status_".$bonus_type_id);

        foreach ($bonus as $key => $val) {
            $this->queue->rPush($queue_key, $val['bonus_id']);
        }

        return true;

    }

    /**
     * @role 获取可领取红包数
     * @author Jianhe
     * @param $bonus_type_id
     * @return int
     */
    public function getRemainderNumber($bonus_type_id){
        return $this->queue->lLen($bonus_type_id);
    }

    /**
     * @author Min
     * @param $bonus_type_id
     * @param $bonus_id
     * @return int
     */
    public function pushBonusToQueue($bonus_type_id, $bonus_id)
    {
        $queue_key = $this->getQueueKey($bonus_type_id);
        return $this->queue->rPush($queue_key, $bonus_id);
    }

    /**
     * @author Min
     * @param $bonus_type_id
     * @return string
     */
    protected function getQueueKey($bonus_type_id)
    {
        return 'bonus_id_queue_'.$bonus_type_id;
    }

    public function getQueueKey_wait()
    {
        return 'bonus_id_queue_waiting';
    }

    /**
     * @author Min
     * @param $bonus_type_id
     * @return mixed
     */
    protected function getBonusList($bonus_type_id)
    {
        $where = array(
            "bonus_type_id" => $bonus_type_id, "user_id" =>0
        );

         return M('user_bonus')->field('bonus_id')->where($where )->select();
    }

    /**
     * @role 获取可领取红包数
     * @author Jianhe
     * @param $bonus_type_id
     * @return int
     */
    public function getRemainderNumber($bonus_type_id){
        $queue_key = $this->getQueueKey($bonus_type_id);
        return $this->queue->lLen($queue_key);
    }

}